package com.SmsModule.servlet; 

import gonggong.GetJifen;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import util.Constant;
import util.QunfaThree;
import util.UtilDAO;

import com.AddressListModule.bean.KehujifenBean;
import com.BlackPhoneModule.bean.BlackPhoneBean;
import com.BlackPhoneModule.dao.BlackPhoneModuleDao;
import com.SmsModule.bean.TbSmsReceive;
import com.SmsModule.bean.TbSmsSc;
import com.SmsModule.bean.TbSmsSend;
import com.SmsModule.dao.TbSmsScDAO;
import com.SmsModule.dao.TbSmsSendDAO;
import com.UserModule.bean.TbUserBean;
import com.dati.bean.Answer;
import com.dati.bean.DatiMiddle;
import com.dati.bean.DatiType;
import com.dati.dao.DatiDao;
import com.dati.dao.DatiTypeDao;
import com.dianbo.bean.Dianbo;
import com.dianbo.bean.DianboType;
import com.dianbo.dao.DianboDao;
import com.dianbo.dao.DianboTypeDao;
import com.jifen.dao.JifenDao;
import com.jspsmart.upload.SmartUpload;

public class TbSmsServlet extends HttpServlet {


	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		String method = request.getParameter("method");
		String nowDate = Constant.decodeDate();//当前时间
	    
		TbUserBean tbUser=(TbUserBean)request.getSession().getAttribute("User");
		TbSmsSendDAO tbsmssend=new TbSmsSendDAO();
		if(method.equals("upLoad")){
			SmartUpload su = new SmartUpload();
			List<TbSmsSend> saveSmsList = new ArrayList<TbSmsSend>();//要保存的发送短信
			su.setCharset("UTF-8");
			String realImagePath = Constant.decodeString(request
					.getParameter("filePath"));
			String tempPath = request.getRealPath("/")
					+ "upload".replace("\\", "/");
			String result = "";
			File file = new File(tempPath);
			if(!file.isFile()){
				file.mkdirs();
			}
			// 上传初始化
			su.initialize(this.getServletConfig(),request,response);

			// 设定上传限制
			// 1.限制每个上传文件的最大长度。
			su.setMaxFileSize(10000000);
			// 2.限制总上传数据的长度。
			su.setTotalMaxFileSize(20000000);
			// 3.设定允许上传的文件（通过扩展名限制）,仅允许doc,txt文件。
			su.setAllowedFilesList("xls,XLS,txt,TXT");
			try{
				// 4.设定禁止上传的文件（通过扩展名限制）,禁止上传带有exe,bat,jsp,htm,html扩展名的文件和没有扩展名的文件。
				su.setDeniedFilesList("exe,bat,jsp,htm,html");
				// 将上传文件保存到指定目录
				tempPath = tempPath.replace("\\", "/");
				tempPath = tempPath + "/";
				su.upload();
				
				su.save(tempPath);
				tempPath += "\\" + su.getFiles().getFile(0).getFileName();
				tempPath = tempPath.replace("\\", "/");
				tempPath = tempPath.replace("//", "/");
				
				List<String> backPhoneList = new ArrayList<String>();//此为黑名单
				QunfaThree qf = new QunfaThree();
				qf.setBackPhoneList(backPhoneList);
				qf.setTbUser(tbUser);
				qf.setTempPath(tempPath);
				if(su.getFiles().getFile(0).getFileName().contains("xls")||su.getFiles().getFile(0).getFileName().contains("XLS")){
					qf.setTypeId("0");
					qf.run();
				}else if(su.getFiles().getFile(0).getFileName().contains("txt")||su.getFiles().getFile(0).getFileName().contains("TXT")){
					qf.setTypeId("1");
					qf.run();
				}
				
			}catch(Exception e){
				e.printStackTrace();
			}
			request.getSession().setAttribute("result", "短信提交成功!");
			response.sendRedirect("SmsModule/SmsSend.jsp");
		}else if(method.equals("look")){
			request.getSession().setAttribute("result", "");
			response.sendRedirect("SmsModule/SmsSend.jsp");
		}else if(method.equals("save")){//发送短信
			List<TbSmsSend> smsSendList = new ArrayList<TbSmsSend>();
			String txtArea = request.getParameter("txtArea");
			String time = request.getParameter("time");//得到发送类型
			String message = Constant.decodeString(request.getParameter("message"));
			String lastMsgContent = java.net.URLDecoder.decode(message, "UTF-8");
			//当前时间
			Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String[] ch = txtArea.split(";");
			List<BlackPhoneBean> blackPhoneList = new BlackPhoneModuleDao().getAllTbBlackPhone("tb_blackPhone", "", "");
			List<String> blackPhone = new ArrayList<String>();
			if(blackPhoneList!=null&&blackPhoneList.size()>0){
				for(BlackPhoneBean bp:blackPhoneList){
					blackPhone.add(bp.getPhone());
				}
				
			}
			if(time.equals("thistime")){//及时发送
				for(String ph:ch){
					if(!ph.equals("")){
						if(!blackPhone.contains(ph)){
							TbSmsSend smsSend = new TbSmsSend();
							smsSend.setCreateTime(Constant.decodeDate());
							smsSend.setDestAdd(ph);
							smsSend.setIsSendTime(0);
							smsSend.setSendTime(Constant.decodeDate());
							smsSend.setSmsContent(message);
							smsSend.setSmsSendState(0);
							smsSend.setUserId(tbUser.getUserId());
							smsSend.setSeriseId(String.valueOf(tbUser.getOrgNum()));
							smsSendList.add(smsSend);
							
						}
					}
				}
			}else{
				String userDate = request.getParameter("sendTime");
				for(String ph:ch){
					if(!ph.equals("")){
						if(!blackPhone.contains(ph)){
							TbSmsSend smsSend = new TbSmsSend();
							smsSend.setCreateTime(Constant.decodeDate());
							smsSend.setDestAdd(ph);
							smsSend.setIsSendTime(0);
							smsSend.setSendTime(Constant.decodeDate());
							smsSend.setSmsContent(message);
							smsSend.setSmsSendState(0);
							smsSend.setUserId(tbUser.getUserId());
							smsSend.setSeriseId(String.valueOf(tbUser.getOrgNum()));
							smsSendList.add(smsSend);
							
						}
					}
				}
			}
			if(smsSendList!=null&&smsSendList.size()>0){
				boolean bo = new TbSmsSendDAO().addSmsList(smsSendList);
				if(bo){
					request.getSession().setAttribute("result", "短信提交成功!");
				}else{
					request.getSession().setAttribute("result", "短信提交失败!");
				}
			}
			response.sendRedirect("SmsModule/SmsSend.jsp");
		}else if(method.equals("smsSend")){//查看短信发送的记录
			String startTime=Constant.decodeString(request.getParameter("startTime"));
			String endTime=Constant.decodeString(request.getParameter("endTime"));
			String phone=request.getParameter("phone");
			String content=request.getParameter("content");
			String condition="userId="+tbUser.getUserId();
			if(startTime.equals("")){
				startTime= Constant.decodeDateT()+" 00:00:00";
			}
			if(endTime.equals("")){
				endTime= Constant.decodeDateT()+" 23:59:59";
			}
			condition+=getCondition(startTime,endTime,phone,content);
			List<TbSmsSend> smsSendList = new ArrayList<TbSmsSend>();
			smsSendList = new TbSmsSendDAO().getSmsList("tb_sms_send", "createTime", condition);
			request.getSession().setAttribute("smsSendList", smsSendList);
			request.getSession().setAttribute("startTime", startTime);
			request.getSession().setAttribute("endTime", endTime);
			request.getSession().setAttribute("phone", phone);
			request.getSession().setAttribute("content", content);
			request.getSession().setAttribute("result", "");
			response.sendRedirect("SmsModule/SmsList.jsp");
			
		}else if(method.equals("deleteOne")){//单个删除
			String startTime=Constant.decodeString(request.getParameter("startTime"));
			String endTime=Constant.decodeString(request.getParameter("endTime"));
			String smsId = request.getParameter("smsId");
			String phone = request.getParameter("phone");
			String content = Constant.decodeString(request.getParameter("content"));
			boolean bo =UtilDAO.del("tb_sms_send", "smsId", smsId);
			if(bo){
				request.getSession().setAttribute("result", "删除成功!");
			}else {
				request.getSession().setAttribute("result", "删除失败!");
			}
			String condition="userId="+tbUser.getUserId();
			condition+=getCondition(startTime,endTime,phone,content);
			List<TbSmsSend> smsSendList = new ArrayList<TbSmsSend>();
			smsSendList = new TbSmsSendDAO().getSmsList("tb_sms_send", "createTime", condition);
			request.getSession().setAttribute("smsSendList", smsSendList);
			request.getSession().setAttribute("startTime", startTime);
			request.getSession().setAttribute("endTime", endTime);
			request.getSession().setAttribute("phone", phone);
			request.getSession().setAttribute("content", content);
			response.sendRedirect("SmsModule/SmsList.jsp");
		}else if(method.equals("deleteAll")){//批删
			String startTime=Constant.decodeString(request.getParameter("startTime"));
			String endTime=Constant.decodeString(request.getParameter("endTime"));
			String phone = request.getParameter("phone");
			String content = Constant.decodeString(request.getParameter("content"));
			String idList = request.getParameter("idList");
			String smsId="";
			if(!idList.equals("")){
				smsId = idList.substring(0, idList.length()-1);
			}
			String condition=" tb_sms_send where smsId in("+smsId+")";
			boolean bo =UtilDAO.delAll(condition);;
			if(bo){
				request.getSession().setAttribute("result", "删除成功!");
			}else {
				request.getSession().setAttribute("result", "删除失败!");
			}
			String condi="userId="+tbUser.getUserId();
			condi+=this.getCondition(startTime, endTime, phone, content);
			List<TbSmsSend> smsSendList = new ArrayList<TbSmsSend>();
			smsSendList = new TbSmsSendDAO().getSmsList("tb_sms_send", "createTime", condi);
			request.getSession().setAttribute("smsSendList", smsSendList);
			request.getSession().setAttribute("startTime", startTime);
			request.getSession().setAttribute("endTime", endTime);
			request.getSession().setAttribute("phone", phone);
			request.getSession().setAttribute("content", content);
			response.sendRedirect("SmsModule/SmsList.jsp");
			
		}else if(method.equals("search")){//查询操作
			String startTime=Constant.decodeString(request.getParameter("startTime"));
			String endTime=Constant.decodeString(request.getParameter("endTime"));
			String phone = request.getParameter("phone");
			String content = Constant.decodeString(request.getParameter("content"));
			String condi="userId="+tbUser.getUserId();
			condi+=this.getCondition(startTime, endTime, phone, content);
			List<TbSmsSend> smsSendList = new ArrayList<TbSmsSend>();
			smsSendList = new TbSmsSendDAO().getSmsList("tb_sms_send", "createTime", condi);
			request.getSession().setAttribute("smsSendList", smsSendList);
			request.getSession().setAttribute("startTime", startTime);
			request.getSession().setAttribute("endTime", endTime);
			request.getSession().setAttribute("phone", phone);
			request.getSession().setAttribute("content", content);
			request.getSession().setAttribute("result", "");
			response.sendRedirect("SmsModule/SmsList.jsp");
		}else if(method.equals("smsSendRece")){//查看短信接收
			String startTime=Constant.decodeString(request.getParameter("startTime"));
			String endTime=Constant.decodeString(request.getParameter("endTime"));
			String phone = request.getParameter("phone");
			String content = Constant.decodeString(request.getParameter("content"));
			if(startTime.equals("")){
				startTime=Constant.decodeDateT()+" 00:00:00";
			}
			if(endTime.equals("")){
				endTime=Constant.decodeDateT()+" 23:59:59";
			}
			String condition="1=1";
			condition+=getConditionRec(startTime,endTime,phone,content);
			List<TbSmsReceive> receiveList = new ArrayList<TbSmsReceive>();
			receiveList = new TbSmsSendDAO().getSmsRecList("tb_smsReceive", "receiveTime", condition);
			
			request.getSession().setAttribute("receiveList", receiveList);
			request.getSession().setAttribute("startTime", startTime);
			request.getSession().setAttribute("endTime", endTime);
			request.getSession().setAttribute("phone", phone);
			request.getSession().setAttribute("content", content);
			request.getSession().setAttribute("result", "");
			response.sendRedirect("SmsModule/SmsReceiveList.jsp");
		}else  if(method.equals("deleteOneRec")){//单个删除接收短信
			String startTime=Constant.decodeString(request.getParameter("startTime"));
			String endTime=Constant.decodeString(request.getParameter("endTime"));
			String smsId = request.getParameter("smsId");
			String phone = request.getParameter("phone");
			String content = Constant.decodeString(request.getParameter("content"));
			boolean bo =UtilDAO.del("tb_smsReceive", "receiveId", smsId);
			if(bo){
				request.getSession().setAttribute("result", "删除成功!");
			}else {
				request.getSession().setAttribute("result", "删除失败!");
			}
			String condition="1=1";
			condition+=getConditionRec(startTime,endTime,phone,content);
			List<TbSmsReceive> receiveList = new ArrayList<TbSmsReceive>();
			receiveList = new TbSmsSendDAO().getSmsRecList("tb_smsReceive", "receiveTime", condition);
			
			request.getSession().setAttribute("receiveList", receiveList);
			request.getSession().setAttribute("startTime", startTime);
			request.getSession().setAttribute("endTime", endTime);
			request.getSession().setAttribute("phone", phone);
			request.getSession().setAttribute("content", content);
			request.getSession().setAttribute("result", "");
			response.sendRedirect("SmsModule/SmsReceiveList.jsp");
		}else if(method.equals("deleteAllRec")){//批量删除
			String startTime=Constant.decodeString(request.getParameter("startTime"));
			String endTime=Constant.decodeString(request.getParameter("endTime"));
			String phone = request.getParameter("phone");
			String content = Constant.decodeString(request.getParameter("content"));
			String idList = request.getParameter("idList");
			String smsId="";
			if(!idList.equals("")){
				smsId = idList.substring(0, idList.length()-1);
			}
			String condition=" tb_smsReceive where receiveId in("+smsId+")";
			boolean bo =UtilDAO.delAll(condition);;
			if(bo){
				request.getSession().setAttribute("result", "删除成功!");
			}else {
				request.getSession().setAttribute("result", "删除失败!");
			}
			String condi="1=1";
			condi+=getConditionRec(startTime,endTime,phone,content);
			List<TbSmsReceive> receiveList = new ArrayList<TbSmsReceive>();
			receiveList = new TbSmsSendDAO().getSmsRecList("tb_smsReceive", "receiveTime", condi);
			
			request.getSession().setAttribute("receiveList", receiveList);
			request.getSession().setAttribute("startTime", startTime);
			request.getSession().setAttribute("endTime", endTime);
			request.getSession().setAttribute("phone", phone);
			request.getSession().setAttribute("content", content);
			response.sendRedirect("SmsModule/SmsReceiveList.jsp");
		}else if(method.equals("searchRec")){///查询接收
			String startTime=Constant.decodeString(request.getParameter("startTime"));
			String endTime=Constant.decodeString(request.getParameter("endTime"));
			String phone = request.getParameter("phone");
			String content = Constant.decodeString(request.getParameter("content"));
			String condi="1=1";
			condi+=getConditionRec(startTime,endTime,phone,content);
			List<TbSmsReceive> receiveList = new ArrayList<TbSmsReceive>();
			receiveList = new TbSmsSendDAO().getSmsRecList("tb_smsReceive", "receiveTime", condi);
			
			request.getSession().setAttribute("receiveList", receiveList);
			request.getSession().setAttribute("startTime", startTime);
			request.getSession().setAttribute("endTime", endTime);
			request.getSession().setAttribute("phone", phone);
			request.getSession().setAttribute("content", content);
			request.getSession().setAttribute("result", "");
			response.sendRedirect("SmsModule/SmsReceiveList.jsp");
		}else if("fenjian".equals(method)){//短信分拣
			String smsId = request.getParameter("smsId");
			String sendPhone = request.getParameter("sendPhone");
			String smsType = request.getParameter("smsType");
			String startTime = Constant.decodeString(request.getParameter("startTime"));
			System.out.println("startTime=="+startTime);
			String endTime = Constant.decodeString(request.getParameter("endTime"));
			
			System.out.println("endTime=="+endTime);
			String content = Constant.decodeString(request.getParameter("content")); 
			String oldPhone= Constant.decodeString(request.getParameter("phone"));
			
			KehujifenBean kehujifen = new JifenDao().getKehujifen(sendPhone); 
			String totalJifen="0";//此号码原有分数
			if(kehujifen==null){
				totalJifen="0";
			}else{
				totalJifen = kehujifen.getCustomerIntegral();
			}
			List<DianboType> dianboTypeList = new ArrayList<DianboType>();
			List<DatiType> datiTypeList = new ArrayList<DatiType>();
			String conditionDianbo = "userId="+tbUser.getUserId();
			String conditionDati="userId = "+tbUser.getUserId();
			dianboTypeList = new DianboTypeDao().getDianboList("tb_qsType_answer", conditionDianbo, "createTime");
			datiTypeList = new DatiTypeDao().getAllDaTiType("tb_answer_type", "createTime", conditionDati);
			request.getSession().setAttribute("dianboTypeList", dianboTypeList);
			request.getSession().setAttribute("datiTypeList", datiTypeList);
			request.getSession().setAttribute("sendPhone", sendPhone);	
			
			request.getSession().setAttribute("startTime", startTime);
			request.getSession().setAttribute("endTime", endTime);
			request.getSession().setAttribute("content", content);
			request.getSession().setAttribute("oldPhone", oldPhone);
			
			
			response.sendRedirect("SmsModule/fenjianSms.jsp?smsId="+smsId+"&num="+new Random().nextInt(10000000)+"&smsType="+smsType+"&totalJifen="+totalJifen);
		}else if("smsUpload".equals(method)){//上传上来的短信
			String startTime=Constant.decodeString(request.getParameter("startTime"));
			String endTime=Constant.decodeString(request.getParameter("endTime"));
			String phone = request.getParameter("phone");
			String content = Constant.decodeString(request.getParameter("content"));
			if(startTime.equals("")){
				startTime=Constant.decodeDateT()+" 00:00:00";
			}
			if(endTime.equals("")){
				endTime=Constant.decodeDateT()+" 23:59:59";
			}	
			System.out.println("startTIme=="+startTime+"&&endTime=="+endTime);
			List<TbSmsSc> receiveList= this.getSmsScList(startTime, endTime, phone, content);
			
			request.getSession().setAttribute("receiveList", receiveList);
			request.getSession().setAttribute("startTime", startTime);
			request.getSession().setAttribute("endTime", endTime);
			request.getSession().setAttribute("phone", phone);
			request.getSession().setAttribute("content", content);
			request.getSession().setAttribute("result", "");
			response.sendRedirect("SmsModule/SmsSCList.jsp");
		}else if("searchRecSc".equals(method)){
			String startTime=Constant.decodeString(request.getParameter("startTime"));
			String endTime=Constant.decodeString(request.getParameter("endTime"));
			String phone = request.getParameter("phone");
			String content = Constant.decodeString(request.getParameter("content"));
			
			List<TbSmsSc> receiveList= this.getSmsScList(startTime, endTime, phone, content);
			request.getSession().setAttribute("receiveList", receiveList);
			request.getSession().setAttribute("startTime", startTime);
			request.getSession().setAttribute("endTime", endTime);
			request.getSession().setAttribute("phone", phone);
			request.getSession().setAttribute("content", content);
			request.getSession().setAttribute("result", "");
			response.sendRedirect("SmsModule/SmsSCList.jsp");
		}else if("deleteAllRecSc".equals(method)){
			String startTime=Constant.decodeString(request.getParameter("startTime"));
			String endTime=Constant.decodeString(request.getParameter("endTime"));
			String phone = request.getParameter("phone");
			String content = Constant.decodeString(request.getParameter("content"));
			String allSmsId[] = request.getParameterValues("deleteAll");
			String smsId="";
			if(allSmsId.length>0){
				for(String id:allSmsId){
					smsId+=id+",";
				}
				smsId = smsId.substring(0, smsId.length()-1);
			}
			String condition=" tb_smsRec_sc where smsId in("+smsId+")";
			boolean bo =UtilDAO.delAll(condition);;
			if(bo){
				request.getSession().setAttribute("result", "删除成功!");
			}else {
				request.getSession().setAttribute("result", "删除失败!");
			}
			List<TbSmsSc> receiveList= this.getSmsScList(startTime, endTime, phone, content);
			
			request.getSession().setAttribute("receiveList", receiveList);
			request.getSession().setAttribute("startTime", startTime);
			request.getSession().setAttribute("endTime", endTime);
			request.getSession().setAttribute("phone", phone);
			request.getSession().setAttribute("content", content);
			response.sendRedirect("SmsModule/SmsSCList.jsp");
		}else if("deleteOneRecSc".equals(method)){
			String startTime=Constant.decodeString(request.getParameter("startTime"));
			String endTime=Constant.decodeString(request.getParameter("endTime"));
			String smsId = request.getParameter("smsId");
			String phone = request.getParameter("phone");
			String content = Constant.decodeString(request.getParameter("content"));
			boolean bo =UtilDAO.del("tb_smsRec_sc", "smsId", smsId);
			if(bo){
				request.getSession().setAttribute("result", "删除成功!");
			}else {
				request.getSession().setAttribute("result", "删除失败!");
			}
			List<TbSmsSc> receiveList= this.getSmsScList(startTime, endTime, phone, content);
			
			request.getSession().setAttribute("receiveList", receiveList);
			request.getSession().setAttribute("startTime", startTime);
			request.getSession().setAttribute("endTime", endTime);
			request.getSession().setAttribute("phone", phone);
			request.getSession().setAttribute("content", content);
			request.getSession().setAttribute("result", "");
			response.sendRedirect("SmsModule/SmsSCList.jsp");
		}else if("fenjianSubmit".equals(method)){//分拣提交操作
			
			String dianboOrDati = request.getParameter("dianboOrDati");//判断是点播还是答题？
			String phone = request.getParameter("phone");
			String fenshu = request.getParameter("fenshu");
			String smsId = request.getParameter("smsId");
			TbSmsSc smsSc = new TbSmsScDAO().getTbSmsSc(Integer.parseInt(smsId));
			if(dianboOrDati.equals("0")){//dianbo
				Dianbo dianbo = new Dianbo();
				String dianboType = request.getParameter("dianboType");
				dianbo.setQsContent(smsSc.getSmscontent());
				dianbo.setQsIntegral("");
				dianbo.setQsUpPhone(phone);
				dianbo.setQsUpTime(Constant.decodeDate());
				dianbo.setReceiveId(Integer.parseInt(smsId));
				dianbo.setTypeId(dianboType);
				String condition=" tb_qs_info where receiveId="+smsId+" and qsUpPhone='"+phone+"'";
				boolean boo = UtilDAO.delAll(condition);
				boolean bo = false;
				if(boo){
					bo = new DianboDao().addDianbo(dianbo);
					if(bo){
						request.getSession().setAttribute("result", "短信分拣成功!");
					}else{
						request.getSession().setAttribute("result", "短信分拣失败!");
					}
				}else{
					bo = new DianboDao().addDianbo(dianbo);
					boolean bol = new GetJifen().saveJifenToCustomer(phone, fenshu);
					request.getSession().setAttribute("result", "短信分拣成功!");
				}
				
			}else if(dianboOrDati.equals("1")){//答题
				String dati_Type = request.getParameter("dati_Type");
				String xilie_Type = request.getParameter("xilie_Type");
				String cond = " receiveId="+smsId+" and answerUpPhone='"+phone+"'";
				List<Answer> answerList = new DatiDao().getAnswerList("tb_answer", "", cond);
				boolean bo = false;
				String content = smsSc.getSmscontent();
				String lastContent = content.substring(0,content.length()-1);//题目
				String rightAnswer = content.substring(content.length()-1);//正确答案 
				if(answerList!=null&&answerList.size()>0){
					String middleId = answerList.get(0).getAnswerMiddleId();
					int answerId = answerList.get(0).getAnswerId();
					boolean bol = UtilDAO.del("tb_answer", "answerId", answerId);
					if(bol){
						bo = UtilDAO.del("tb_answer_middle", "answerMiddleId", middleId);//删除中间表
						if(bo){
							DatiMiddle datiMid = new DatiMiddle();
							String num = Constant.getNumT();
							datiMid.setAnswerContent(lastContent);
							datiMid.setAnswerMiddleId(num);
							datiMid.setCreateTime(Constant.decodeDate());
							datiMid.setRightAnswer(rightAnswer);
							datiMid.setSeriseId(xilie_Type);
							boolean bool= new DatiDao().saveDatiMiddle(datiMid);
							if(bool){
								Answer  answer = new Answer();
								answer.setAnswerIntegral("");
								answer.setAnswerMiddleId(num);
								answer.setAnswerUpPhone(phone);
								answer.setAnswerUpTime(Constant.decodeDate());
								answer.setReceiveId(Integer.parseInt(smsId));
								boolean bl = new DatiDao().saveAnswer(answer);
								if(bl){
									request.getSession().setAttribute("result", "短信分拣成功!");
									//boolean bll = new GetJifen().saveJifenToCustomer(phone, fenshu);
								}else{
									request.getSession().setAttribute("result", "短信分拣失败!");
								}
							}
						}else{
							request.getSession().setAttribute("result", "短信分拣失败!");
						}
					}
				}else{
					DatiMiddle datiMid = new DatiMiddle();
					String num = Constant.getNumT();
					datiMid.setAnswerContent(lastContent);
					datiMid.setAnswerMiddleId(num);
					datiMid.setCreateTime(Constant.decodeDate());
					datiMid.setRightAnswer(rightAnswer);
					datiMid.setSeriseId(xilie_Type);
					boolean bool= new DatiDao().saveDatiMiddle(datiMid);
					if(bool){
						Answer  answer = new Answer();
						answer.setAnswerIntegral("");
						answer.setAnswerMiddleId(num);
						answer.setAnswerUpPhone(phone);
						answer.setAnswerUpTime(Constant.decodeDate());
						answer.setReceiveId(Integer.parseInt(smsId));
						boolean bl = new DatiDao().saveAnswer(answer);
						if(bl){
							request.getSession().setAttribute("result", "短信分拣成功!");
							boolean bll = new GetJifen().saveJifenToCustomer(phone, fenshu);
						}else{
							request.getSession().setAttribute("result", "短信分拣失败!");
						}
					}
				
				}
			
				
			}
			
			
			String startTime=Constant.decodeString(request.getParameter("startTime"));
			String endTime=Constant.decodeString(request.getParameter("endTime"));
			String content = Constant.decodeString(request.getParameter("content"));
			String oldPhone = Constant.decodeString(request.getParameter("oldPhone"));
			
			List<TbSmsSc> receiveList= this.getSmsScList(startTime, endTime, oldPhone, content);
			
			request.getSession().setAttribute("receiveList", receiveList);
			request.getSession().setAttribute("startTime", startTime);
			request.getSession().setAttribute("endTime", endTime);
			request.getSession().setAttribute("phone", oldPhone);
			request.getSession().setAttribute("content", content);
			
			response.sendRedirect("SmsModule/SmsSCList.jsp");
		}else if("sendAgin".equals(method)){//短信重发
			String smsId = request.getParameter("smsId");
			String sql="update tb_sms_send set smsSendState=0,createTime='"+Constant.decodeDate()+"',sendTime='"+Constant.decodeDate()+"' where smsId="+smsId;
			boolean bo = new TbSmsSendDAO().updateSmsSend(sql);
			
			String phone="";
			String content="";
			String condition="userId="+tbUser.getUserId();
			String startTime=Constant.decodeDateT()+" 00:00:00";
			String endTime = Constant.decodeDateT()+" 23:59:59";
			condition+=getCondition(startTime,endTime,phone,content);
			List<TbSmsSend> smsSendList = new ArrayList<TbSmsSend>();
			smsSendList = new TbSmsSendDAO().getSmsList("tb_sms_send", "createTime", condition);
			request.getSession().setAttribute("smsSendList", smsSendList);
			request.getSession().setAttribute("startTime", startTime);
			request.getSession().setAttribute("endTime", endTime);
			request.getSession().setAttribute("phone", phone);
			request.getSession().setAttribute("content", content);
			if(bo){
				request.getSession().setAttribute("result", "重发成功!");
			}else{
				request.getSession().setAttribute("result", "重发失败!");
			}
			
			response.sendRedirect("SmsModule/SmsList.jsp");
			
		}
	}//end doPost

	public List<TbSmsSc> getSmsScList(String startTime,String endTime,String phone,String content){
		String condi="1=1";
		condi+=getConditionRecSC(startTime,endTime,phone,content);
		List<TbSmsSc> receiveList = new ArrayList<TbSmsSc>();
		receiveList = new TbSmsSendDAO().getSmsSc("tb_smsRec_sc", "createTime", condi);
		return receiveList;
	}
	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public String getCondition(String startTime,String endTime,String phone,String content){
		String condition="";
	
		if(startTime.equals("")&&endTime.equals("")&&phone.equals("")&&content.equals("")){
			
		}else if(!startTime.equals("")&&endTime.equals("")&&phone.equals("")&&content.equals("")){
			condition+=" and createTime >='"+startTime+"'";
		}else if(startTime.equals("")&&!endTime.equals("")&&phone.equals("")&&content.equals("")){
			condition+=" and createTime<='"+endTime+"'";
		}else if(startTime.equals("")&&endTime.equals("")&&!phone.equals("")&&content.equals("")){
			condition+=" and destAdd='"+phone+"'";
		}else if(startTime.equals("")&&endTime.equals("")&&phone.equals("")&&!content.equals("")){
			condition+="smsContent like '%"+content+"%'";
		}else if(!startTime.equals("")&&!endTime.equals("")&&phone.equals("")&&content.equals("")){
			condition+=" and createTime >='"+startTime+"' and createTime<='"+endTime+"'";
		}else if(!startTime.equals("")&&endTime.equals("")&&!phone.equals("")&&content.equals("")){
			condition+=" and createTime >='"+startTime+"' and destAdd='"+phone+"'";
		}else if(!startTime.equals("")&&endTime.equals("")&&phone.equals("")&&!content.equals("")){
			condition+=" and createTime >='"+startTime+"' and smsContent like '%"+content+"%'";
		}else if(startTime.equals("")&&!endTime.equals("")&&!phone.equals("")&&content.equals("")){
			condition+=" and destAdd='"+phone+"' and createTime<='"+endTime+"'";
		}else if(startTime.equals("")&&!endTime.equals("")&&phone.equals("")&&!content.equals("")){
			condition+=" and createTime<='"+endTime+"' and  smsContent like '%"+content+"%'";
		}else if(startTime.equals("")&&endTime.equals("")&&!phone.equals("")&&!content.equals("")){
			condition+=" and destAdd='"+phone+"' and  smsContent like '%"+content+"%'";
		}else if(!startTime.equals("")&&!endTime.equals("")&&!phone.equals("")&&content.equals("")){
			condition+=" and createTime >='"+startTime+"' and createTime<='"+endTime+"' and destAdd='"+phone+"'";
		}else if(!startTime.equals("")&&endTime.equals("")&&!phone.equals("")&&!content.equals("")){
			condition+=" and createTime >='"+startTime+"' and destAdd='"+phone+"' and  smsContent like '%"+content+"%'";
		}else if(startTime.equals("")&&!endTime.equals("")&&!phone.equals("")&&!content.equals("")){
			condition+=" and destAdd='"+phone+"' and createTime<='"+endTime+"' and  smsContent like '%"+content+"%'";
		}else if(!startTime.equals("")&&!endTime.equals("")&&!phone.equals("")&&!content.equals("")){
			condition+=" and createTime >='"+startTime+"' and createTime<='"+endTime+"' and destAdd='"+phone+"' and  smsContent like '%"+content+"%'";
		}else if(!startTime.equals("")&&!endTime.equals("")&&phone.equals("")&&!content.equals("")){
			condition+=" and createTime >='"+startTime+"' and createTime<='"+endTime+"' and  smsContent like '%"+content+"%'";
		}
		return condition;
	}
	public String getConditionRec(String startTime,String endTime,String phone,String content){
		String condition="";
		Date startDate = Timestamp.valueOf(startTime);
		Date endDate = Timestamp.valueOf(endTime);
		
		if(!phone.equals("")&&content.equals("")){
			condition+=" and sendPhone='"+phone+"' and receiveTime>='"+startDate+"' and receiveTime<='"+endDate+"'";
		}else if(phone.equals("")&&!content.equals("")){
			condition+=" and receiveContent like '%"+content+"%' and receiveTime>='"+startDate+"' and receiveTime<='"+endDate+"'";
		}else if(!phone.equals("")&&!content.equals("")){
			condition+=" and  receiveContent like '%"+content+"%' and sendPhone='"+phone+"' and receiveTime>='"+startDate+"' and receiveTime<='"+endDate+"'";
		}else {
			condition+=" and receiveTime>='"+startDate+"' and receiveTime<='"+endDate+"'";
		}
		return condition;
	}
	
	/**
	 * 
	 *@author qingyu zhang
	 *@function:上传的信息
	 * @param startTime
	 * @param endTime
	 * @param phone
	 * @param content
	 * @return
	 *2011-4-12
	 *下午10:54:02
	 *zzClub
	 *com.SmsModule.servlet
	 *String
	 */
	public String getConditionRecSC(String startTime,String endTime,String phone,String content){
		String condition="";
		Date startDate = Timestamp.valueOf(startTime);
		Date endDate = Timestamp.valueOf(endTime);
		
		if(!phone.equals("")&&content.equals("")){
			condition+=" and sendPhone='"+phone+"' and createTime>='"+startDate+"' and createTime<='"+endDate+"'";
		}else if(phone.equals("")&&!content.equals("")){
			condition+=" and smscontent like '%"+content+"%' and createTime>='"+startDate+"' and createTime<='"+endDate+"'";
		}else if(!phone.equals("")&&!content.equals("")){
			condition+=" and  smscontent like '%"+content+"%' and sendPhone='"+phone+"' and createTime>='"+startDate+"' and createTime<='"+endDate+"'";
		}else {
			condition+=" and createTime>='"+startDate+"' and createTime<='"+endDate+"'";
		}
		return condition;
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
		System.out.println("init方法");
	}

	 
}
